package com.itheima.stock.listener;

import com.github.benmanes.caffeine.cache.Cache;
import com.itheima.stock.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 监听股票变化消息
 */
@Component
@Slf4j
public class MqListener {
    @Resource
    private Cache<String,Object> caffeineCache;
    @Resource
    private StockService stockService;
    @RabbitListener(queues = "innerMarketQueue")
    public void innerMarketListener(Date date){

        //获取当前时间与接收到消息的时间差
        long diffTime = DateTime.now().getMillis() - new DateTime(date).getMillis();
        if (diffTime > 60000){
            log.info("消息接收时间过长，当前时间：{}，延时：{}ms",DateTime.now(),diffTime);
        }
        //将本地缓存的一些数据置为失效
        caffeineCache.invalidate("innerMarket");

        //通知消费之刷新本地缓存
        stockService.getInnerMarketInfo();

    }

}
